# -*- coding: utf-8 -*-
import sys
import os
import time
import pygame
import pytmx
import actor.hero as hero
import scene.castle as castle
import tools.game_const as game_const
from pytmx.util_pygame import load_pygame

pygame.init()
pygame.display.set_caption('英雄传奇')
show_screen = pygame.display.set_mode((game_const.display_width, game_const.display_height))
path = os.path.join('resource','tmx', "castle.tmx")
map = pytmx.TiledMap(path)
display_width = map.width * map.tilewidth
display_height = map.height * map.tileheight
game_screen = pygame.Surface((display_width, display_height))
game_map = load_pygame(path)

count = 0
layer_count = 0
show_layer_count = 0
ti = game_map.get_tile_image_by_gid
for layer in game_map.visible_layers:
    layer_count += 1
    if isinstance(layer, pytmx.TiledTileLayer):
        show_layer_count += 1
        count = 0
        for x, y, gid in layer:
            tile = ti(gid)
            if tile and isinstance(tile, pygame.Surface):
                game_screen.blit(tile, (x * game_map.tilewidth, y * game_map.tileheight))
                count += 1

        print(count)

print("======")
print(layer_count)
print(show_layer_count)


clock = pygame.time.Clock()
gameExit = False
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

    show_screen.blit(game_screen, (0,0))

    pygame.display.update()
    clock.tick(30)

pygame.quit()